/*
MIT License

Copyright (c) 2025 sidianneng(bbear_mail@163.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include "driver/gpio.h"

#include "max17048.h"
#include "battery.h"

#define LED_BAT100 19
#define LED_BAT75  18
#define LED_BAT50  17
#define LED_BAT25  16
#define LED_BAT0   9
#define CHG_STAT   0

#define I2C_MASTER_SCL_IO          (gpio_num_t)14         /*!< gpio number for I2C master clock */
#define I2C_MASTER_SDA_IO          (gpio_num_t)15         /*!< gpio number for I2C master data  */

static i2c_bus_handle_t i2c_bus = NULL;
static max17048_handle_t max17048 = NULL;

static void max17048_init(void)
{
    i2c_config_t conf = {
        .mode = I2C_MODE_MASTER,
        .sda_io_num = I2C_MASTER_SDA_IO,
        .sda_pullup_en = GPIO_PULLUP_ENABLE,
        .scl_io_num = I2C_MASTER_SCL_IO,
        .scl_pullup_en = GPIO_PULLUP_ENABLE,
        .master.clk_speed = 400 * 1000,
    };
    i2c_bus = i2c_bus_create(I2C_NUM_0, &conf);
    max17048 = max17048_create(i2c_bus, MAX17048_I2C_ADDR_DEFAULT);
}

static void battery_task(void* arg)
{
    float percent = 0;
    uint32_t temp = 0;
    for(;;) {
        vTaskDelay(750 / portTICK_PERIOD_MS);
        max17048_get_cell_percent(max17048, &percent);
        printf("percent:%.2f %%\n", percent);
        if(percent >= 75.0) {
            gpio_set_level(LED_BAT100, 0);
            gpio_set_level(LED_BAT75, 0);
            gpio_set_level(LED_BAT50, 0);
            gpio_set_level(LED_BAT25, 0);
            gpio_set_level(LED_BAT0, 1);
            temp = LED_BAT100;
        } else if(percent < 75.0 && percent >= 50.0) {
            gpio_set_level(LED_BAT100, 1);
            gpio_set_level(LED_BAT75, 0);
            gpio_set_level(LED_BAT50, 0);
            gpio_set_level(LED_BAT25, 0);
            gpio_set_level(LED_BAT0, 1);
            temp = LED_BAT75;
        } else if(percent < 50.0 && percent >= 25.0) {
            gpio_set_level(LED_BAT100, 1);
            gpio_set_level(LED_BAT75, 1);
            gpio_set_level(LED_BAT50, 0);
            gpio_set_level(LED_BAT25, 0);
            gpio_set_level(LED_BAT0, 1);
            temp = LED_BAT50;
        } else if(percent < 25.0 && percent >= 15.0) {
            gpio_set_level(LED_BAT100, 1);
            gpio_set_level(LED_BAT75, 1);
            gpio_set_level(LED_BAT50, 1);
            gpio_set_level(LED_BAT25, 0);
            gpio_set_level(LED_BAT0, 1);
            temp = LED_BAT25;
        } else if(percent < 15.0 && percent >= 0.0) {
            gpio_set_level(LED_BAT100, 1);
            gpio_set_level(LED_BAT75, 1);
            gpio_set_level(LED_BAT50, 1);
            gpio_set_level(LED_BAT25, 1);
            gpio_set_level(LED_BAT0, 0);
            temp = LED_BAT0;
        } 


        if(!gpio_get_level(CHG_STAT)){
            gpio_set_level(temp, 0);
            vTaskDelay(250 / portTICK_PERIOD_MS);
            gpio_set_level(temp, 1);
        } else
            vTaskDelay(250 / portTICK_PERIOD_MS);
    }
}

esp_err_t battery_init(void)
{
    gpio_config_t io_conf = {};
    io_conf.intr_type = GPIO_INTR_DISABLE;
    io_conf.mode = GPIO_MODE_OUTPUT;
    io_conf.pin_bit_mask = ((1ULL<<LED_BAT100) | (1ULL<<LED_BAT75) | (1ULL<<LED_BAT50) | (1ULL<<LED_BAT25) \
                            | (1ULL<<LED_BAT0));
    io_conf.pull_down_en = (gpio_pulldown_t)1;
    io_conf.pull_up_en = (gpio_pullup_t)0;
    gpio_config(&io_conf);

    gpio_reset_pin(CHG_STAT);
    gpio_set_direction(CHG_STAT, GPIO_MODE_INPUT);
    gpio_set_pull_mode(CHG_STAT, GPIO_PULLUP_ONLY);

    gpio_set_level(LED_BAT100, 1);
    gpio_set_level(LED_BAT75, 1);
    gpio_set_level(LED_BAT50, 1);
    gpio_set_level(LED_BAT25, 1);
    gpio_set_level(LED_BAT0, 1);

    max17048_init();

    xTaskCreate(battery_task, "battery_task", 4096, NULL, 10, NULL);

    return ESP_OK;
}

